package com.sanhuo.idms.service;

import com.sanhuo.idms.bo.RepairSaveBO;
import com.sanhuo.idms.bo.RepairSearchBO;
import com.sanhuo.idms.dao.RepairMapper;
import com.sanhuo.idms.entity.Repair;
import com.sanhuo.idms.entity.RepairPersonal;
import com.sanhuo.idms.enums.RepairStateEnum;
import com.sanhuo.idms.vo.RepairPageVO;
import com.sanhuo.lib.ExceptionUtil;
import com.sanhuo.security.UserVo;
import com.sanhuo.security.uitls.UserHolder;
import com.sanhuo.triild.Basic.BaseMapper;
import com.sanhuo.triild.Basic.BaseService;
import com.sanhuo.triild.annotation.MapperWire;
import com.sanhuo.triild.page.Page;
import com.sanhuo.triild.page.PageSearch;
import com.sanhuo.utils.basic.BeanUtil;
import com.sanhuo.utils.basic.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class RepairService extends BaseService<Repair> {
    @MapperWire
    private RepairMapper repairMapper;
    @Autowired
    private RepairPersonalService repairPersonalService;

    @Override
    public BaseMapper<Repair> getBaseMapper() {
        return repairMapper;
    }

    public void save(final UserVo user, RepairSaveBO bo) throws IllegalAccessException {
        Repair repair = new Repair();
        BeanUtil.copyBean(bo, repair);
        repair.setId(StringUtil.UUID());
        repair.creatorAndUpdatorInfo(user);
        repair.setState(RepairStateEnum.INAUDIT.getCode());
        save(repair);
    }

    public Page<RepairPageVO> listAll(final UserVo user, PageSearch pageSearch) {
        if ("03".equals(user.getRole().getId())) {
            RepairSearchBO bo = new RepairSearchBO();
            bo.setUserId(user.getId());
            return repairMapper.listAll(bo, pageSearch);

        } else {
            RepairSearchBO bo = new RepairSearchBO();
            return repairMapper.listAll(bo, pageSearch);
        }
    }

    public List<RepairPageVO> findByIds(List<String> ids) {
        return repairMapper.findByIds(ids);
    }

    public void changeState(final UserVo user, String id) {
        Repair repair = findById(id);
        ExceptionUtil.throwException(BeanUtil.isNull(repair), "不存在该维修任务记录!");
        repair.setState(RepairStateEnum.REFER.getCode());
        repair.updatorInfo(user);
        update(repair);
    }

    public void deal(final UserVo user, String id) {
        Repair repair = findById(id);
        ExceptionUtil.throwException(BeanUtil.isNull(repair), "不存在该维修任务记录!");
        RepairPersonal personal = repairPersonalService.findById(user.getId());
        personal.setTaskNum(personal.getTaskNum() + 1);
        personal.updatorInfo(user);
        repairPersonalService.update(personal);
        repair.setState(RepairStateEnum.FINISHED.getCode());
        repair.updatorInfo(user);
        update(repair);
    }

    public Integer countUnPass() {
        return repairMapper.countUnPass(RepairStateEnum.INAUDIT.getCode());
    }


}
